<?php
//error_reporting(E_COMPILE_ERROR|E_ERROR|E_CORE_ERROR);
require('./roots.php');
require($root_path.'include/inc_environment_global.php');
/**
* CARE2X Integrated Hospital Information System beta 2.0.1 - 2004-07-04
* GNU General Public License
* Copyright 2002,2003,2004,2005,2006 Elpidio Latorilla
* elpidio@care2x.org, 
*
* See the file "copy_notice.txt" for the licence notice
*/
$lang=$_GET['lang'];
$lang_tables[] = 'interconsulta.php';
define('LANG_FILE','history.php');
define('NO_2LEVEL_CHK',1);
require_once($root_path.'include/inc_front_chain_lang.php');

global $_POST;

$thisfile=basename(__FILE__);
$update = $_POST['update'];

if (!isset($update)||empty($update))
{
    $update = $_GET['update'];
}

require_once($root_path.'include/care_api_classes/class_core.php');
$core=new Core;

//TODO: insertar/actualizar en la tabla de historia
if ($update) {
        
        

        require_once($root_path.'include/care_api_classes/class_gui_history.php');
        global $_POST;
		
        $guihistory = new GuiHistory($_POST['ficha']);
        $temp = $guihistory->updateDataFromArray();

        //         echo "<pre>";
        //         var_dump($_POST);
        //         echo "</pre>";

        $files = explode(' ', $listaArchivos);
	foreach($files as $fileInput ){
		$guihistory->guardarAdjunto(trim($fileInput), $_POST['id']);
	}
		//var_dump($guihistory);
        
        if ($temp==NULL || $temp==FALSE)$temp="F";
        else if ($temp==TRUE) $temp="V";
		if($temp!=NULL){
                    
                    header("Location:$root_path/modules/registration_admission/patient_register_show.php".URL_REDIRECT_APPEND."&lang=$lang&pid=$guihistory->pid&v=".$temp."");
                    exit;

                }else{
                    header("Location:$root_path/modules/registration_admission/patient_register_show.php".URL_REDIRECT_APPEND."&lang=$lang&pid=$guihistory->pid");
                    exit;
                }
} else  { //Insercion de historia

        //AQUI VERIFICO PERMISO DE INSERT

        require_once($root_path.'include/care_api_classes/class_gui_history.php');
        global $lang, $_POST;

        $guihistory = new GuiHistory($_POST['ficha']);
        $temp = $guihistory->insertDataFromInternalArray();
        //var_dump($temp);
        $files = explode(' ', $listaArchivos);
	foreach($files as $fileInput ){
	  $guihistory->guardarAdjunto(trim($fileInput), (($_POST['id'])?($_POST['id']):($guihistory->last_id)));
	}

	//var_dump($guihistory);
        if ($temp==NULL || $temp==FALSE)$temp="F";
        else if ($temp==TRUE) $temp="V";
		if($temp!=NULL){
                    header("Location:$root_path/modules/registration_admission/patient_register_show.php".URL_REDIRECT_APPEND."&lang=$lang&pid=$guihistory->pid&v=".$temp."");
                    exit;

                }else{
                    header("Location:$root_path/modules/registration_admission/patient_register_show.php".URL_REDIRECT_APPEND."&lang=$lang&pid=$guihistory->pid");
                    exit;
                }

        //var_dump($temp);
}


?>
